package de.s9mtdev.lol_destiny;

import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class InSession extends Activity {

	boolean online = true;
	int currentUsers = 1;
	Messenger messenger = null;
	RiftView riftview;
	Hashtable<String, FlashView> flashviews = new Hashtable<String, FlashView>(5);
	Hashtable<String, BuffView> buffviews = new Hashtable<String, BuffView>(4);
	Hashtable<String, BossView> bossviews = new Hashtable<String, BossView>(2);
	final Handler delay_handler = new Handler();
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			Bundle b = msg.getData();
			if (b.getString("info") != null
					&& b.getString("info").equals("online")) {
				online = true;
			} else if (b.getString("info") != null
					&& b.getString("info").equals("offline")) {
				online = false;
			} else if (b.getString("info") != null
					&& b.getString("info").equals("join")) {
				currentUsers++;	
				updateTextView();
			} else if (b.getString("info") != null
					&& b.getString("info").equals("part")) {
				currentUsers--;
				updateTextView();
			} else if (b.getString("say") != null) {
				String d = b.getString("say");
				if (d.startsWith("ward")) {
					riftview.setWard(Integer.valueOf(d.substring(5,
							d.lastIndexOf(" "))), Integer.valueOf(d.substring(d
							.lastIndexOf(" ") + 1)));
					System.out.println("ward request from irc");
				} else if (d.startsWith("unward")) {
					riftview.unsetWard(Integer.valueOf(d.substring(7,
							d.lastIndexOf(" "))), Integer.valueOf(d.substring(d
							.lastIndexOf(" ") + 1)));
				} else if (d.startsWith("flash")) {
					flashviews.get(d.substring(6)).setFlash();
				} else if (d.startsWith("buff")) {
					buffviews.get(d.substring(5)).setBuff();
				} else if (d.startsWith("boss")) {
					bossviews.get(d.substring(5)).setBoss();
				}				
			}
		}
	};

	private ServiceConnection conn = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder binder) {
			messenger = new Messenger(binder);
		}
		public void onServiceDisconnected(ComponentName className) {
			messenger = null;
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_session);
		updateTextView();

		Intent service = new Intent(this, NetworkService.class);
		bindService(service, conn, Context.BIND_AUTO_CREATE);
		
		riftview = (RiftView) findViewById(R.id.riftView);
		FlashView flashview1 = (FlashView) findViewById(R.id.flashView1);
		FlashView flashview2 = (FlashView) findViewById(R.id.flashView2);
		FlashView flashview3 = (FlashView) findViewById(R.id.flashView3);
		FlashView flashview4 = (FlashView) findViewById(R.id.flashView4);
		FlashView flashview5 = (FlashView) findViewById(R.id.flashView5);
		flashviews.put(flashview1.getTag().toString(), flashview1);		
		flashviews.put(flashview2.getTag().toString(), flashview2);
		flashviews.put(flashview3.getTag().toString(), flashview3);
		flashviews.put(flashview4.getTag().toString(), flashview4);
		flashviews.put(flashview5.getTag().toString(), flashview5);
		
		BuffView buffview1 = (BuffView) findViewById(R.id.buffView1);
		BuffView buffview2 = (BuffView) findViewById(R.id.buffView2);
		BuffView buffview3 = (BuffView) findViewById(R.id.buffView3);
		BuffView buffview4 = (BuffView) findViewById(R.id.buffView4);
		buffviews.put(buffview1.getTag().toString(), buffview1);
		buffviews.put(buffview2.getTag().toString(), buffview2);
		buffviews.put(buffview3.getTag().toString(), buffview3);
		buffviews.put(buffview4.getTag().toString(), buffview4);
		
		BossView bossview1 = (BossView) findViewById(R.id.bossView1);
		BossView bossview2 = (BossView) findViewById(R.id.bossView2);
		bossviews.put(bossview1.getTag().toString(), bossview1);
		bossviews.put(bossview2.getTag().toString(), bossview2);
	}

	@Override
	public void onStart() {
		super.onStart();
		Timer t = new Timer();
		t.schedule(new TimerTask() {
			public void run() {
				delay_handler.post(new Runnable() {
					public void run() {
						Message msg = Message.obtain();
						try {
							Bundle bundle = new Bundle();
							bundle.putString("hello", "service");
							msg.setData(bundle);
							msg.replyTo = new Messenger(handler);
							messenger.send(msg);
							System.out.println("hello service sent");
						} catch (RemoteException e1) {
							e1.printStackTrace();
						}
					}
				});
			}
		}, 5000);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		unbindService(conn);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.layout_session, menu);
		return true;
	}
	
	public void updateTextView() {
		TextView sessioninfo = (TextView) findViewById(R.id.textView_session);
		sessioninfo.setText(getIntent().getExtras().getString("session") + " - Users: " + currentUsers +"/5");
	}

	public void exitSession(View view) {
		sendMsg("exit", "exit");
		finish();
	}

	public void sendMsg(String a, String b) {
		Message msg = Message.obtain();
		try {
			Bundle bundle = new Bundle();
			bundle.putString(a, b);
			msg.setData(bundle);
			messenger.send(msg);
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
	}
}
